//
//  SearchViewIcon.m
//  SearchView
//
//  Created by 冷悦 on 16/3/1.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "SearchViewIcon.h"
@interface SearchViewIcon()
@property (nonatomic, strong) CAShapeLayer *circleLayer;
@property (nonatomic, strong) CAShapeLayer *circleHandleLayer;
@end

@implementation SearchViewIcon

#pragma mark - Private

/**
 *  计算以一点为圆心，给定角度与半径，在圆上的位置
 *
 *  @param center 圆心
 *  @param angle  给定的角度，使用0-360，而不是3.1415926方式
 *  @param radius 半径
 *
 *  @return 圆上的点
 */
- (CGPoint) calcCircleCoordinateWithCenter:(CGPoint)center
                                 withAngle:(CGFloat)angle
                                 andRadius:(CGFloat)radius{
    CGFloat x = radius * cosf(angle * M_PI / 180);
    CGFloat y = radius * sinf(angle * M_PI / 180);
    
    return CGPointMake(center.x + x, center.y + y);
}

- (void)calculateCircleCenterPoint:(CGPoint)centerPoint andRadius:(CGFloat)radius{
    CGFloat circleOffset = radius / 5.0;
    CGFloat circleRadius = circleOffset * 3.0;
    CGPoint circleCenterPoint = [self calcCircleCoordinateWithCenter:centerPoint
                                                           withAngle:225
                                                           andRadius:circleOffset];
    
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:circleCenterPoint
                                                              radius:circleRadius
                                                          startAngle:0
                                                            endAngle:M_PI * 2
                                                           clockwise:YES];
    
    CGPoint circleHandleBeginPoint = [self calcCircleCoordinateWithCenter:circleCenterPoint
                                                                withAngle:45
                                                                andRadius:circleRadius];
    
    UIBezierPath *circleHandlePath = [UIBezierPath new];
    [circleHandlePath moveToPoint:circleHandleBeginPoint];
    CGFloat handleLength = radius;
    
    CGPoint circleHandleEndPoint = [self calcCircleCoordinateWithCenter:centerPoint
                                                              withAngle:45
                                                              andRadius:handleLength];
    [circleHandlePath addLineToPoint:circleHandleEndPoint];
    self.circleLayer.path = circlePath.CGPath;
    self.circleLayer.fillColor = [UIColor clearColor].CGColor;
    
#ifndef kSearchViewIconLayerColor
#define kSearchViewIconLayerColor [UIColor colorWithWhite:0.651 alpha:1.000]
#endif
    CGColorRef shapeColor = self.shapeColor==nil ?
        kSearchViewIconLayerColor.CGColor : self.shapeColor.CGColor;
    
#ifndef kSearchViewIconLineWidth
#define kSearchViewIconLineWidth circleRadius / 3.0
#endif
    
    CGFloat lineWidth = self.lineWidth <= 0 ?
        kSearchViewIconLineWidth : self.lineWidth;
    self.circleLayer.strokeColor = shapeColor;
    self.circleLayer.lineWidth = lineWidth;
    
    self.circleHandleLayer.path = circleHandlePath.CGPath;
    self.circleHandleLayer.fillColor = [UIColor clearColor].CGColor;
    self.circleHandleLayer.strokeColor = shapeColor;
    self.circleHandleLayer.lineWidth = lineWidth;
    self.circleHandleLayer.lineCap = kCALineJoinRound;
}

#pragma mark - Override
- (void)drawRect:(CGRect)rect {
    CGFloat width = CGRectGetWidth(rect);
    CGFloat height = CGRectGetHeight(rect);
    CGFloat radius = MIN(width, height) / 2.0;
    CGPoint centerPoint = CGPointMake(width / 2.0, height / 2.0);
    [self calculateCircleCenterPoint:centerPoint andRadius:radius];
    
}

#pragma mark - Setters
- (void)setShapeColor:(UIColor *)shapeColor{
    _shapeColor = shapeColor;
    [self setNeedsDisplay];
}

- (void)setLineWidth:(CGFloat)lineWidth{
    _lineWidth = lineWidth;
    [self setNeedsDisplay];
}

#pragma mark - Getters
- (CAShapeLayer *)circleLayer{
    if (_circleLayer == nil) {
        _circleLayer = [CAShapeLayer new];
        [self.layer addSublayer:_circleLayer];
    }
    
    return _circleLayer;
}

- (CAShapeLayer *)circleHandleLayer{
    if (_circleHandleLayer == nil) {
        _circleHandleLayer = [CAShapeLayer new];
        [self.layer addSublayer:_circleHandleLayer];
    }
    
    return _circleHandleLayer;
}

@end
